package com.framework.seedkit.http.okhttp;

import com.framework.seedkit.utils.LOG;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by ekimatsu on 16/11/30.
 * OkHttpHelper  网络请求类,供应用程序调用,主要提供了get与post的访问方式。
 */

public class OkHttpHelper {
    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");
    OkHttpClient client = new OkHttpClient();

    /**
     * 描述: get异步请求
     * @param url 请求地址
     * @param callback 请求返回后的回调
     * return void
     */
    public void get(String url, final HttpStringCallback callback) {
        Request request = new Request.Builder()
                .url(url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (callback != null) {
                    callback.onError();
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (callback != null) {
                    callback.onSuccess(response.toString());
                }
            }
        });
    }

    /**
     * 描述: post异步请求
     * @param url 请求地址
     * @param jsonObject 请求参数Json对象
     * @param callback 请求返回后的回调
     * return void
     */
    public void post(String url, JSONObject jsonObject, final HttpStringCallback callback) {
        RequestBody body = RequestBody.create(JSON, jsonObject.toString());
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callback.onError();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callback.onSuccess(response.toString());
            }
        });
    }

    /**
     * 描述: get同步请求
     * @param url 请求地址
     * return Response 返回数据报文
     */
    public Response get(String url) {
        Response response = null;
        Request request = new Request.Builder()
                .url(url)
                .build();

        try {
            response = client.newCall(request).execute();
        } catch (IOException e) {
            LOG.d("synchronize get failed,IOException occur!");
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 描述: post同步请求
     * @param url 请求地址
     * @param jsonObject 请求参数Json对象
     * return Response 返回数据报文
     */
    public Response post(String url, JSONObject jsonObject) {
        Response response = null;
        RequestBody body = RequestBody.create(JSON, jsonObject.toString());
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try{
            response = client.newCall(request).execute();
        } catch (IOException e) {
            LOG.d("synchronize post failed,IOException occur!");
            e.printStackTrace();
        }

        return response;
    }
}
